// 引入接口文件
import { getPromoteList, queryAllAddress, houseListGet,exportFile} from './registeredUserStatisticsServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download,makeAddress } from 'utils/util'


const queryList = [

  { name: '推荐时间', type: 'date', key: 'date', value: [] ,options: { format: 'YYYY-MM-DD',width:'500' }},
]

// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  queryList,
  currentItem: {},
  modalObj:{
    visible:false,
  },
  productSelf: [],
  productIds: [],
  // 表格数据
  tableData: {
    dataSource: []
  },
  // 表格分页设置
  pageConfig: {
    pageSize: Number.parseInt(config.smallPageSizeList[0]),
    pageNo: 1,
  },

}

// 声明module
const tmpModule = {
  // namespace: 'productListModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
          // 执行业务操作
          dispatch({type: 'search', payload: {}}) ;// end dispatch
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    //查询操作
    * search ({payload = {}}, {call, put, select}) {
      let {queryList, tableData, pageConfig} = yield select(state => state[tmpModule.namespace])
      //显示loading状态
      yield put({type: 'updateStore', payload: {loading: true, btnLoading: true,}})
      //组装接口入参
      let params = {
        startTime: null,
        endTime: null,
      }
    
      queryList.map(item => {
        let {key = null, value = null} = item
        if (value === '') {value = null}//如果没有值，置为null
        if (item.key == 'date') {
          if (!isEmpty(value)) {
            params.startTime = value[0]
            params.endTime = value[1]
          }
        }
     
      })
      
      const {success,data,msg} = yield call(getPromoteList,params)
      
      // console.log('数据',data)
      if (!success) {
        yield put({type: 'updateStore', payload: {loading: false, btnLoading: false,}})
        message.error(msg)
        return false
      }
      // var arr = [data]
      console.log(data);
      let list = [{data}];//[0]
      list.map((item, index) => {
        item.key = index + 1//
      })
      tableData.dataSource=list
      yield put({
        type: 'updateStore', payload: {
          loading: false,
          btnLoading: false,
          pageConfig: {...pageConfig, ...payload},
          tableData,
        }
      })
    },
    
    //改变查询条件
    *inputChange({ payload }, { call, put, select }) {
      let { queryList } = yield select(state => state[tmpModule.namespace])
      const { key, value } = payload;
      let data = queryList.find(item => item.key === key);
      data.value = value;
      yield put({ type: 'updateStore', payload: { queryList: [...queryList, ...data] } });
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule